/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cageballserver.scoreboard;

import cageballcommon.enummerations.TeamEnum;
import cageballcommon.exceptions.CageballMatchException;
import cageballserver.CageballClient;
import cageballserver.Game;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kanonas
 */
public class ScoreBoardEntry implements Comparable {

    transient private CageballClient team;
    transient private List<Game> games;
    private String teamName;
    private int points;
    private int scores;
    private int concedes;
    private int wins;
    private int draws;
    private int defeats;

    public ScoreBoardEntry(CageballClient team) {
        this.team = team;
        teamName = team.getInitTeam().getTeamName();
        points = 0;
        scores = 0;
        concedes = 0;
        wins = 0;
        draws = 0;
        defeats = 0;

        games = new ArrayList<Game>();
    }

    public void update(TeamEnum myTeam, Game game) {

        games.add(game);

        if (myTeam == game.getWinner()) {
            points += 3;
            wins ++;
        } else if (game.getWinner() == TeamEnum.none || game.getWinner() == TeamEnum.both) {
            points += 1;
            defeats ++;
        } else{
            draws ++;
        }

        try {
            scores += game.getMyScore(myTeam);
            concedes += game.getOtherScore(myTeam);
        } catch (CageballMatchException ex) {
            Logger.getLogger(ScoreBoardEntry.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getConcedes() {
        return concedes;
    }

    public int getPoints() {
        return points;
    }

    public int getScores() {
        return scores;
    }

    public String getTeamName() {
        return teamName;
    }

    public CageballClient getTeam() {
        return team;
    }

    public int getDefeats() {
        return defeats;
    }

    public int getDraws() {
        return draws;
    }

    public int getWins() {
        return wins;
    }
    
    private int checkMatch(CageballClient otherTeam) {
        for (Game game : games) {
            if (game.getTeam1().equals(team) && game.getTeam2().equals(otherTeam)) {
                if (game.getWinner() == TeamEnum.myTeam) {
                    return 1;
                } else if (game.getWinner() == TeamEnum.otherTeam) {
                    return -1;
                }
                return 0;
            } else if (game.getTeam1().equals(otherTeam) && game.getTeam2().equals(team)) {
                if (game.getWinner() == TeamEnum.myTeam) {
                    return -1;
                } else if (game.getWinner() == TeamEnum.otherTeam) {
                    return 1;
                }
                return 0;
            }
        }

        return 0;
    }

    @Override
    public int compareTo(Object o) {
        ScoreBoardEntry other = (ScoreBoardEntry) o;

        if (points > other.getPoints()) {
            return 1;
        } else if (points < other.getPoints()) {
            return -1;
        } else {
            if ((scores - concedes) > (other.getScores() - other.getConcedes())) {
                return 1;
            } else if ((scores - concedes) < (other.getScores() - other.getConcedes())) {
                return -1;
            } else {
                if (scores > other.getScores()) {
                    return 1;
                } else if (scores < other.getScores()) {
                    return -1;
                } else {
                    return checkMatch(other.getTeam());
                }
            }
        }
    }
}
